<?php
namespace App\Models\AppModels;
use App\Models\ParentModel;
use DB;
use App\Components\Tools;
/**
 * 直播数据model
 * @author puppet
 */
class UserLiveVideo extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_user_live';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_user_live';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;

    /*
     * 把获取到的数据存库
     * */
	public function saveLiveApiInfo($data){
		$xx=$data['xx'];
		$this->db_insertGetId($data);
	}
    public function getindex(){
        $sql=" select * from $this->table where status>=0";
        $re=DB::select($sql);
        return $re;
    }
    public function checkRoomid($id,$roomid){
        $sql=" select id from pt_user_live where id='$id' and room_id='$roomid'";
        $re = DB::select($sql);
        return isset($re[0]['id']) ? $re : false;;
	}
	
	
	//添加、更新直播雷达
	function saveLive($data,$values){
		$sql="select id from pt_user_live where room_id='{$values[0]}' and system_type={$values[1]} and partner_id=?";
		$old=DB::select($sql,[$values[2]]);
		$old=isset($old[0]['id']) ? $old[0]['id'] : false;
		if($old){
			return DB::table('pt_user_live')->where('id',$old)->update($data);
		}else{
			$data['room_id']=$values[0];
			$data['system_type']=$values[1];
			$data['partner_id']=$values[2];
			$data['create_time']=date('Y-m-d H:i:s');
			return DB::table('pt_user_live')->insertGetId($data);
		}
	}
	
	
    /**
     *	获取直播监控数据
     *	$type 1(主页用户)  2(单视频用户) 3(广告用户)   $page 第几页   $role_id角色id   partner_id登录的ID
     */
    function getLiveData($parames){
		// echo "<pre/>";print_r($parames);exit;
        $partner_id=isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
        $name=isset($parames['name']) ? $parames['name']:'';
        $size = (isset($parames['limit']) && intval($parames['limit'])>0 && intval($parames['limit']) <= 100) ? $parames['limit'] : 10;
        $page = isset($parames['page']) ? $parames['page'] : 1;
        $offset = $size * ($page - 1);
        $countSql=" select count(id) as count from pt_user_live where partner_id = ? ";
        $sql=" select id,partner_id,room_id,info_name,info_type,create_time,update_time,status,room_url from pt_user_live where partner_id = ?";
        
		if(isset($name) && !empty($name) ){
            $countSql.=" and info_name like '%{$name}%'";
            $sql.=" and info_name like '%{$name}%'";
        }
		
		$sql.=" order by status desc,update_time desc limit $offset,$size";
        $count =DB::select($countSql,[$partner_id]);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        $data=DB::select($sql,[$partner_id]);
		
        foreach ($data as $k=>$v){
			$statusText=$v['status']==0 ? 'default' : 'checkbox';
			$checkedText=$v['status']==0 ? '' : 'checked';
			$data[$k]['status']="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusText}\" checked=\"{$checkedText}\" onclick=\"infoStatus({$v['id']},this)\"><span></span></label></div></td>";
			
            $data[$k]['allCount']="7 / 1";
            $data[$k]['room_url']="<a href=\"{$v['room_url']};\" target=\"_blank\" style=\"color:#00b7ee;\">".mb_substr($v['room_url'],0,10)."...".mb_substr($v['room_url'],-10)."</a>";
			
            $data[$k]['operation']="<a href=\"javascript:;\" title=\"查看评论\"><span class=\"glyphicon glyphicon-eye-open\" onclick=\"showContent({$v['id']},'{$v['info_name']}')\"></span></a>";
            $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;<a href=\"javascript:;\" title=\"查看用户\"><span class=\"fa fa-navicon\" onclick=\"showUsers({$v['id']},'{$v['info_name']}')\"></span></a>";
        }
        return ['data'=>$data,'total'=>$count];
    }
	
	//拉取直播评论
	function liveComment($video_id,$page,$filter,$weekTime,$gende){
		$size = 20;
		$offset = $size * ($page - 1);
		$countSql="select count(id) from pt_user_live_comment where live_id=?";
		$sql=" select a.*,b.room_url from pt_user_live_comment as a left join pt_user_live as b on b.id=a.live_id";
		$sql.=" where a.live_id=?";
		
		if($filter && $filter!=99){
			$countSql.=" and isFilter={$filter}";
			$sql.=" and a.isFilter={$filter}";
		}
		if($weekTime && $weekTime==1){
			$countSql.=" and create_time>='".date('Y-m-d H:i:s',time()-3600)."'";
			$sql.=" and a.create_time>='".date('Y-m-d H:i:s',time()-3600)."'";
		}elseif($weekTime && $weekTime==2){
			$countSql.=" and create_time>='".date('Y-m-d H:i:s',time()-24*3600)."'";
			$sql.=" and a.create_time>='".date('Y-m-d H:i:s',time()-24*3600)."'";
		}elseif($weekTime && $weekTime==3){
			$countSql.=" and create_time>='".date('Y-m-d H:i:s',time()-7*24*3600)."'";
			$sql.=" and a.create_time>='".date('Y-m-d H:i:s',time()-7*24*3600)."'";
		}
		if($gende && $gende!=99){
			$countSql.=" and gende={$gende}";
			$sql.=" and a.gende={$gende}";
		}
		
		$sql.=" order by a.update_time desc limit {$offset},{$size}";
		$count=DB::select($countSql,[$video_id]);
		$count=isset($count[0]['count']) ? $count[0]['count'] : 0;
		$re=DB::select($sql,[$video_id]);
		$re=isset($re[0]['id']) ? $re : false;
		
		$html="";
		$nextPage=$page < ceil($count/$size) ? $page+1 : 0;
		if($re){
			foreach($re as $k=>$v){
				$html.="<tr>";
					$html.="<td><a href=\"https://www.douyin.com/user/{$v['sec_uid']}\" target=\"_blank\"><img src=\"{$v['userImg']}\" width=\"50px\" height=\"50px\"></a></td>";
					$html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['content']}</td>";
					$html.="<td>{$v['nickname']}</td>";
					$html.="<td>{$v['short_id']}</td>";
					$html.="<td><a href=\"{$v['room_url']}\" target=\"_blank\">".mb_substr($v['room_url'],0,20)."...".mb_substr($v['room_url'],-10)."</a></td>";
                    $html.="<td>".mb_substr($v['create_time'],0,22)."</td>";
                    $html.="<td>".mb_substr($v['update_time'],0,10)."</td>";
					$statusText=$v['status']==0 ? "default" : "checkbox";
					$checkedText=$v['status']==0 ? "" : "checked";
					$html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusText}\" checked=\"{$checkedText}\" onclick=\"upStatus({$v['id']},this)\"><span></span></label></div></td>";
					$follow="snssdk1128://user/profile/".$v['uid'];
					$html.="<td><a href=\"javascript:void(0);\" title=\"扫码私聊\"><span class=\"btn btn-info\" onclick=\"following('{$follow}')\">扫码私聊</span></a></td>";
				$html.="</tr>";
			}
		}
		return ["html"=>$html,'nextPage'=>$nextPage];
	}
    //拉取直播评论
    function liveinfoComment($video_id,$page,$filter,$weekTime,$gende){
        $size = 20;
        $offset = $size * ($page - 1);
        $countSql="select count(id) from pt_user_live_comment where live_id=?";
        $sql=" select a.*,b.room_url from pt_user_live_comment as a left join pt_user_live as b on b.id=a.live_id";
        $sql.=" where a.live_id=?";

        if($filter && $filter!=99){
            $countSql.=" and isFilter={$filter}";
            $sql.=" and a.isFilter={$filter}";
        }
        if($weekTime && $weekTime==1){
            $countSql.=" and create_time>='".date('Y-m-d H:i:s',time()-3600)."'";
            $sql.=" and a.create_time>='".date('Y-m-d H:i:s',time()-3600)."'";
        }elseif($weekTime && $weekTime==2){
            $countSql.=" and create_time>='".date('Y-m-d H:i:s',time()-24*3600)."'";
            $sql.=" and a.create_time>='".date('Y-m-d H:i:s',time()-24*3600)."'";
        }elseif($weekTime && $weekTime==3){
            $countSql.=" and create_time>='".date('Y-m-d H:i:s',time()-7*24*3600)."'";
            $sql.=" and a.create_time>='".date('Y-m-d H:i:s',time()-7*24*3600)."'";
        }
        if($gende && $gende!=99){
            $countSql.=" and gende={$gende}";
            $sql.=" and a.gende={$gende}";
        }

        $sql.=" order by a.update_time desc limit {$offset},{$size}";
        $count=DB::select($countSql,[$video_id]);
        $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
        $re=DB::select($sql,[$video_id]);
        $re=isset($re[0]['id']) ? $re : false;

        $nextPage=$page < ceil($count/$size) ? $page+1 : 0;

        return ["data"=>$re,'nextPage'=>$nextPage];
    }
	//查询评论状态
	function selectComStatus($kid){
		$re=DB::select("select status from pt_user_live_comment where id={$kid}");
		return isset($re[0]['status']) ? $re[0]['status'] : 99;
	}
	
	//修改评论状态
	function saveStatus($kid,$data){
		return DB::table('pt_user_live_comment')->where('id',$kid)->update($data);
	}



    //修改询盘评论 处理状态
    function editStatus($kid,$status){
        return DB::table('pt_user_live_comment')->where('id',$kid)->update(['status'=>$status]);
    }
	
	
	
	
	
	
	
    //获取潜在的客户 全部用户/筛选用户
    function getcustomer(){

    }
}